package com.panshi.service.impl;

import com.panshi.mapper.ITitleMapper;
import com.panshi.service.ITitleService;
import com.panshi.vo.qa.Title;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TitleService implements ITitleService {

    @Autowired
    ITitleMapper titleMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean addTitle(Title title) {
        try {
            Title t = titleMapper.queryByNameTitle(title);
            if(t == null){
                titleMapper.addTitle(title);
                return true;
            }
            return false;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    @Cacheable(value = "title:queryTitle",key = "#title")
    public List<Title> queryTitle(Title title) {
        List<Title> lsit = titleMapper.queryTitle(title);
        return lsit;
    }

    @Override
    public Title queryByNameTitle(Title title) {
        Title t = titleMapper.queryByNameTitle(title);
        return t;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)//spring事务注解型
    public boolean updateTitle(Title title) {
        try {
            titleMapper.updateTitle(title);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean forbidUseTitle(int tid) {
        try {
            titleMapper.forbidUseTitle(tid);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public boolean startUseTitle(int tid) {
        try {
            titleMapper.startUseTitle(tid);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
